<?php

namespace App\Model\Backend;

use Illuminate\Database\Eloquent\Model;

class ItemTasksModel extends Model
{
    protected $table = 'items_tasks';
    protected $primaryKey = 'id';

//工作台任务展示
    public function showTasks($id){


    $tasks=$this->where(['member_id_task'=>$id])->where('task_status','>=',0)->where('task_status','<=',10)->orderBy('task_status','DESC')->orderBy('id','ASC')->get();
    $item=new ItemModel();
    $member=new Member();
    $arr=array();
    foreach($tasks as $k=>$v){
        $itemInfo =$item->select(['item_name','member_id'])->find($v['item_id']);
        $n=$member->find($itemInfo['member_id']);
        $re=$this->where(['item_id'=>$v['item_id']])->get();
        foreach($re as $i=>$y){
            if($y['id']==$v['id']){
                $arr[$v['item_id']]['common'][$i]=$y;
            }
        }
        $arr[$v['item_id']]['item_name']=$itemInfo['item_name'];
        $arr[$v['item_id']]['username']=$n['username'];
        $arr[$v['item_id']]['item_id']=$v['item_id'];

    }
    ksort($arr);
    return $arr;
}
    //工作台审核任务
    public function showCheck($id){

        $tasks=$this->Where(['member_id_check'=>$id,'task_status'=>20])->orderBy('id','ASC')->get();
        $item=new ItemModel();
        $member=new Member();
        $arr=array();

        foreach($tasks as $k=>$v){
            $itemInfo =$item->select(['item_name','member_id'])->find($v['item_id']);
            $n=$member->find($itemInfo['member_id']);
            $re=$this->where(['item_id'=>$v['item_id']])->get();
            foreach($re as $i=>$y){
                if($y['id']==$v['id']){
                    $arr[$v['item_id']]['common'][$i]=$y;
                }
            }
            $arr[$v['item_id']]['item_name']=$itemInfo['item_name'];
            $arr[$v['item_id']]['username']=$n['username'];
            $arr[$v['item_id']]['item_id']=$v['item_id'];

        }
        ksort($arr);
        return $arr;
    }
    //通知下个任务人
    public function nextMessage($item_id,$task_level){

        $r=$this->where(['item_id'=>$item_id])->where(['task_level'=>$task_level+1])->orderBy('task_level','ASC')->first();
        $res=$this->where(['item_id'=>$item_id,'task_level'=>$r['task_level']])->get();
        $userID="";
        $qiyehao=new QiyehaoModel();
        foreach($res as $k=>$v){
            $userID .=Member::find($v['member_id_task'])['wechat']."|";
            $log=new TaskLogModel();
            $j=$this->find($v['id']);
            $j->task_status=10;
            $j->task_starttime=time();
            $j->save();
            $log->item_task_id=$v['id'];
            $log->action="任务开始";
            $log->save();
        }

        $content="【".ItemModel::find($item_id)['item_name']."  需要您的协助，请至工作台查看。】";
        $qiyehao->text(trim($userID,'|'),$content);
       // return $userID;
       // return 456;
        return true;
    }

    public function deleteTask($id){


       return $this->where(['id'=>$id])->orWhere(['fid'=>$id])->update(['task_status'=>"-10"]);
    }

    //撤回删除状态从-10变成0
    public function backTask($id){

        return $this->where(['id'=>$id])->orWhere(['fid'=>$id])->update(['task_status'=>"0"]);
    }
}
